package com.authine.cloudpivot.ext.applicationservice.basic;

import cn.hutool.core.lang.Assert;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.alibaba.cola.exception.Exceptions;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.utils.BoServiceUtils;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import com.authine.mvp.app.launcher.dto.errorcode.ErrorCodeEnum;
import com.authine.mvp.app.launcher.rpc.SaveOrUpdateBO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.Collections;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * @ClassName ClientService
 * @Description: 认证服务客户端自定义服务
 * @Author fj
 * @Date 2021/8/31
 * @Version V1.0
 **/
@Path("ClientService")
@Slf4j
public class ClientService {
    /**
     * 新增客户端
     *
     * @param param 客户端新增数据
     * @return 响应结果
     */
    @Path("saveClient")
    @POST
    @Operation(summary = "新增客户端", tags = CUSTOM_SERVICE)
    public SingleResponse<BO> saveClient(@Parameter SaveOrUpdateBO param) {
        Assert.notNull(param.getData().get("secret"), "客户端密码不能为空");
        // 密码加密
        String secret = String.valueOf(param.getData().get("secret"));
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(secret);
        param.getData().put("secret", encode);
        // 新增客户端
        log.info("------------------------新增客户端数据：{}------------------------", JSONObject.toJSONString(param.getData()));
        SingleResponse<BO> clientCreateBO = BoServiceUtils.createBo(ModelConst.T_CLIENT, Collections.singletonList(param.getData()));
        if (!clientCreateBO.isSuccess()) {
            Exceptions.throwBizException(ErrorCodeEnum.BO_SAVE_ERROR);
        }
        return clientCreateBO;
    }

    /**
     * 修改客户端
     *
     * @param param 客户端修改数据
     * @return 响应结果
     */
    @Path("updateClient")
    @POST
    @Operation(summary = "修改客户端", tags = CUSTOM_SERVICE)
    public SingleResponse<BO> updateClient(@Parameter SaveOrUpdateBO param) {
        SingleResponse<BOList> clientBOList = BoServiceUtils.findListBySimple(ModelConst.T_CLIENT, "id", param.getData().get("id"));
        if (clientBOList.getData().getTotal() == 0) {
            throw new BizException("ID:" + param.getData().get("id") + "不存在");
        }
        // 修改密码
        if (!StringUtils.isEmpty(param.getData().get("secret"))) {
            String secret = String.valueOf(param.getData().get("secret"));
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(secret);
            param.getData().put("secret", encode);
        } else {
            // 避免密码被覆盖
            param.getData().remove("secret");
        }
        // 修改客户端
        log.info("------------------------修改客户端数据：{}------------------------", JSONObject.toJSONString(param.getData()));
        SingleResponse<BO> clientUpdateBO = BoServiceUtils.updateMainBo(ModelConst.T_CLIENT, param.getData());
        if (!clientUpdateBO.isSuccess()) {
            Exceptions.throwBizException(ErrorCodeEnum.BO_UPDATE_ERROR);
        }
        return clientUpdateBO;
    }
}
